package com.example.demo01.entity.dto;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.demo01.config.StudentExcel;
import com.example.demo01.entity.Student;
import lombok.Getter;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ImportStudentDTO extends AnalysisEventListener<StudentExcel> {

    @Getter
    private final List<Student> studentList = new ArrayList<>();
    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/M/d");

    @Override
    public void invoke(StudentExcel data, AnalysisContext context) {
        Student student = new Student();
        student.setName(data.getName());
        student.setStudentNo(data.getStudentNo());
        student.setGender("男".equals(data.getGender()) ? Boolean.TRUE : Boolean.FALSE);
        student.setStatus("在读".equals(data.getStatus()) ? Boolean.TRUE : Boolean.FALSE);

        try {
            student.setBirthDate(LocalDate.parse(data.getBirthDate(), formatter));
            student.setJoinDate(LocalDate.parse(data.getJoinDate(), formatter));
        } catch (Exception e) {
            // 可记录错误行号或跳过
        }

        student.setCreatedAt(java.time.LocalDateTime.now());
        student.setUpdatedAt(java.time.LocalDateTime.now());

        studentList.add(student);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 可选：全部解析完成后处理
    }

}